package com.traffic.api.filter.filter;

import java.util.Map;

import com.gxhl.api.core.tools.ToolSecuritySHA;
import com.traffic.api.exception.ApiBizException;
import com.traffic.api.jedis.JedisClient;

public class SignFilter implements IApiFilter {
	public static String cacheStart_appkey = "appkey_";

	public void validateFilter(Map<String, Object> params) throws ApiBizException {
		if ((params.get("sig") == null) || ("".equals(params.get("sig")))) {
			throw new ApiBizException(new Throwable("(api)您填写的sig不能为空"));
		}
		String sgin = (String) params.get("sig");

		params.remove("sig");

		String ip = "";

		if (params.containsKey("ip")) {
			ip = (String) params.get("ip");
			params.remove("ip");
		}

		String pramsOrder = ToolSecuritySHA.dataDecrypt(params);
		JedisClient client = JedisClient.getInstance();
		String privateKey = (String) client.get(cacheStart_appkey + params.get("appkey"));

		String sig = ToolSecuritySHA.shaEncrypt(pramsOrder + privateKey);

		if (!(sig.equals(sgin))) {
			throw new ApiBizException(new Throwable("(api)您填写的sig错误"));
		}
		params.put("ip", ip);
	}
}